﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace Bixas.Tangibilis.Controls.Primitives
{
    public class TouchRepeatButton : RepeatButton, ITouchCursorEvent, ITouchCursorEventThunk, ITouchElement
    {
        #region ITouchCursorEvent Members

        public event System.Windows.RoutedEventHandler TouchCursorDown
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorDownEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorDownEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorEnter
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorEnterEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorEnterEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorLeave
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorLeaveEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorLeaveEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorMove
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorMoveEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorMoveEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorUp
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorUpEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorUpEvent, value);
            }
        }
        
        public event System.Windows.RoutedEventHandler TouchCursorPreviewDown
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorPreviewDownEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorPreviewDownEvent, value);
            }
        }
        
        public event System.Windows.RoutedEventHandler TouchCursorPreviewMove
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorPreviewMoveEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorPreviewMoveEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorPreviewUp
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorPreviewUpEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorPreviewUpEvent, value);
            }
        }
        #endregion

        private TouchRepeatButtonAdapter _adapter;

        private static readonly DependencyPropertyKey IsTouchCursorOverPropertyKey;
        public static readonly DependencyProperty IsTouchCursorOverProperty;

        private static readonly DependencyPropertyKey IsTouchCursorPressedPropertyKey;
        public static readonly DependencyProperty IsTouchCursorPressedProperty;
        
        public TouchRepeatButton()
            : base()
        {
            this._adapter = new TouchRepeatButtonAdapter(this);

        }

        static TouchRepeatButton()
        {
            //FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchButton), new FrameworkPropertyMetadata(typeof(TouchButton)));
            IsTouchCursorOverPropertyKey = DependencyProperty.RegisterReadOnly("IsTouchCursorOver", typeof(bool), typeof(TouchRepeatButton), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(TouchRepeatButton.OnIsTouchCursorOverPropertyChanged)));
            IsTouchCursorOverProperty = IsTouchCursorOverPropertyKey.DependencyProperty;

            IsTouchCursorPressedPropertyKey = DependencyProperty.RegisterReadOnly("IsTouchCursorPressed", typeof(bool), typeof(TouchRepeatButton), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(TouchRepeatButton.OnIsTouchCursorPressedPropertyChanged)));
            IsTouchCursorPressedProperty = IsTouchCursorPressedPropertyKey.DependencyProperty;

        }

        #region ITouchCursorEventThunk Members

        public void OnTouchCursorDownThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorDown(e);
        }

        public void OnTouchCursorEnterThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorEnter(e);
        }

        public void OnTouchCursorLeaveThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorLeave(e);
        }

        public void OnTouchCursorMoveThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorMove(e);
        }

        public void OnTouchCursorUpThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorUp(e);
        }

        public void OnTouchCursorPreviewDownThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorPreviewDown(sender, e);
        }

        public void OnTouchCursorPreviewMoveThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorPreviewMove(sender, e);
        }

        public void OnTouchCursorPreviewUpThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorPreviewUp(sender, e);
        }

        #endregion



        internal void CrackedWriteFlag(uint flags, bool value)
        {
            Phydeaux.Utilities.Dynamic<UIElement>.Instance.Procedure.Params.CreateDelegate("WriteFlag").Invoke(this, new object[] { flags, value });

        }


        private static void OnIsTouchCursorOverPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TouchRepeatButton)d).OnTouchCursorOverChanged(e);
        }

        protected virtual void OnTouchCursorOverChanged(DependencyPropertyChangedEventArgs e)
        {
        }

        private static void OnIsTouchCursorPressedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TouchRepeatButton)d).OnTouchCursorPressedChanged(e);
        }

        protected virtual void OnTouchCursorPressedChanged(DependencyPropertyChangedEventArgs e)
        {
        }

        #region ITouchElement Members

        bool ITouchElement.IsTouchCursorOver
        {
            get
            {
                return (bool)base.GetValue(IsTouchCursorOverProperty);
            }
            set
            {
                base.SetValue(IsTouchCursorOverPropertyKey, value);
                // this.CrackedWriteFlag(0x2000, value);
            }
        }

        bool ITouchElement.IsTouchCursorPressed
        {
            get
            {
                return (bool)base.GetValue(IsTouchCursorPressedProperty);
            }
            set
            {
                base.SetValue(IsTouchCursorPressedPropertyKey, value);
                base.SetValue(Phydeaux.Utilities.Dynamic<ButtonBase>.Static.Field<DependencyPropertyKey>.Getter.CreateDelegate("IsPressedPropertyKey").Invoke(),value);
            }
        }


        #endregion
    }
}
